// 云函数入口文件
const cloud = require('wx-server-sdk')

cloud.init({
  env: cloud.DYNAMIC_CURRENT_ENV
})

const db = cloud.database()

// 云函数入口函数
exports.main = async (event, context) => {
  try {
    let count = await db.collection('record').count()
    let total = count.total
    let tasks = []
    
    // 数据量大时，分批次获取
    for (let i = 0; i < total; i += 100) {
      const promise = db.collection('record')
        .skip(i)
        .limit(100)
        .get()
      tasks.push(promise)
    }
    
    // 等待所有数据获取完成
    const results = await Promise.all(tasks)
    
    // 合并数据
    let data = []
    results.forEach(result => {
      data = data.concat(result.data)
    })

    return {
      success: true,
      data: data
    }
  } catch (err) {
    console.error(err)
    return {
      success: false,
      error: err
    }
  }
}